﻿using System;
using ZFine.Application.Gas;
using ZFine.Code;
using ZFine.Domain.Entity.Gas;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace ZFine.Web.Areas.Gas.Controllers
{
    public class DispenserController : ControllerBase
    {
        //
        // GET: /Gas/Dispenser/

        public ActionResult Index()
        {
            return View();
        }

        private DispenserApp  dispenserApp = new DispenserApp();
        private CompanyApp companyApp = new CompanyApp();
        /// <summary>
        /// Index 中填充列表
        /// </summary>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJson(Pagination pagination, string keyword)
        {
            List<DispenserEntity> dispenserEntities = dispenserApp.GetList(pagination, keyword);
            var rows = from dispenser in dispenserEntities
                       join company in companyApp.GetList()
                       on dispenser.CompanyId equals company.F_Id
                       select new
                       {
                           dispenser.F_Id,
                           dispenser.CompanyId,
                           dispenser.DeviceName,
                           dispenser.DeviceSerialNo,
                           dispenser.IPAddress,
                           company.CompanyName
                       };

            var data = new
            {
                rows =rows,
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }
        /// <summary>
        /// Form 公司下拉菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetTreeSelectJson()
        {
            var data =companyApp.GetList();
            var treeList = new List<TreeSelectModel>();
            foreach (CompanyEntity item in data)
            {
                TreeSelectModel treeModel = new TreeSelectModel();
                treeModel.id = item.F_Id;
                treeModel.text = item.CompanyName;
                treeModel.parentId = "0";//应该是多级的，上级id？
                treeModel.data = item;
                treeList.Add(treeModel);
            }
            return Content(treeList.TreeSelectJson());
        }
        /// <summary>
        /// 获得单条数据 填充
        /// </summary>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormJson(string keyValue)
        {
            var data = dispenserApp.GetForm(keyValue);
            return Content(data.ToJson());
        }
        /// <summary>
        /// 提交表单
        /// </summary>
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitForm(DispenserEntity dispenserEntity, string keyValue)
        {
            dispenserApp.SubmitForm(dispenserEntity, keyValue);
            return Success("操作成功。");
        }
        /// <summary>
        /// 删除一项
        /// </summary>
        [HttpPost]
        [HandlerAjaxOnly]
        [HandlerAuthorize]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteForm(string keyValue)
        {
            dispenserApp.DeleteForm(keyValue);
            return Success("删除成功。");
        }
    }
}
